package com.nuoniu.sibanyun.controller.system;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysRole;
import com.nuoniu.sibanyun.entity.SysUserRole;
import com.nuoniu.sibanyun.entity.dto.SysRoleDto;
import com.nuoniu.sibanyun.entity.vo.RoleSimpleVo;
import com.nuoniu.sibanyun.service.ISysMenuService;
import com.nuoniu.sibanyun.service.ISysRoleMenuService;
import com.nuoniu.sibanyun.service.ISysRoleService;
import com.nuoniu.sibanyun.service.ISysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/system/role")
@Api(value = "角色管理", tags = "角色管理")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private ISysUserRoleService userRoleService;

    /**
     * 获取角色列表
     * @author 小啊刚
     * @date 2020/5/27
     */
    @GetMapping("/page")
    public HttpResult getRolePage(SysRoleDto sysRoleDto) {
        return HttpResult.ok(sysRoleService.getRolePage(sysRoleDto));
    }

    /**
     * 获取用户角色id
     * @author 小啊刚
     * @date 2020/6/3
     */
    @GetMapping("/checkRoleId")
    public HttpResult checkRoleId(Long userId) {
        return HttpResult.ok(userRoleService.checkRoleId(userId));
    }

    /**
     * 获取所有角色
     */
    @GetMapping("/list")
    public HttpResult getRoleAll() {
        return HttpResult.ok(sysRoleService.getRoleAll());
    }

    /**
     * 保存角色
     * @author 小啊刚
     * @date 2020/5/27
     */
    @PostMapping("save")
    public HttpResult saveRole(@RequestBody SysRoleDto sysRole) {
        sysRoleService.saveRole(sysRole);
        return HttpResult.ok();
    }

    /**
     * 获取角色权限菜单id
     * @author 小啊刚
     * @date 2020/5/28
     */
    @GetMapping("getRoleMenu")
    public HttpResult getRoleMenu(Long id) {
        return HttpResult.ok(sysMenuService.getByRole(id));
    }

    /**
     * 删除角色
     * @author 小啊刚
     * @date 2020/5/27
     */
    @DeleteMapping("delete")
    public HttpResult deleteRole(Long id) {
       if(id == null) {
           return HttpResult.error("id不能为空");
       }

        sysRoleService.deleteRole(id);
        return HttpResult.ok();
    }
}
